window.addEventListener('load', function(){
	ROOT = new Directory("");
	CURDIR = ROOT;

	var home = new Directory("home");
	ROOT.appendChild(home);

	var readme = new File("readme");
	readme.content = "this is virtual web terminal.";
	ROOT.appendChild(readme);

	var thread = new SpecialDirectory("thread", "/thread/");
	ROOT.appendChild(thread);

	var image = new SpecialDirectory("image", "/repository/");
	ROOT.appendChild(image);

	var kjwon15 = new Directory("kjwon15");
	home.appendChild(kjwon15);

	var program = new Program('program', function(){
		print("Test program.");
		print("Hello, World!");
	});
	kjwon15.appendChild(program);


	//Easter eggs
	var _hyeseong = new Directory(".hyeseong");
	home.appendChild(_hyeseong);

	var readme = new File("readme");
	readme.content = "This directory is only for Hye-seong my lil cat.\nNYA!";
	_hyeseong.appendChild(readme);

	var iloveyou = new Program('iloveyou', function(){
		changeBackground('background2.jpg');
		print('<span style="font-size: 2em;">' +
			"I love you Hye-seong.\nHappy 1st anniversary." +
			'</span>');
		setTimeout(function(){
			objLine.readOnly = true;
			objMain.style.opacity = 0.5;
			var div = document.createElement('div');
			div.style.cssText = "position: absolute;width: 800px; height: 600px;word-break:break-all;";
			div.style.cssText += "transition: opacity 2s";
			div.style.left = objMain.offsetLeft + "px";
			div.style.top = objMain.offsetTop + "px";
			document.body.appendChild(div);

			var spanarr = [];
			spanarr.shuffle = function(){
				for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
			};

			var arr_I = [
				[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1,12],[1,13],
				[2,6],[2,7],[2,8],[2,9],[2,10],[2,11],[2,12],[2,13],
				[3,9],[4,9],[5,9],[6,9],[7,9],[8,9],[9,9],[10,9],[11,9],
				[3,10],[4,10],[5,10],[6,10],[7,10],[8,10],[9,10],[10,10],[11,10],
				[12,6],[12,7],[12,8],[12,9],[12,10],[12,11],[12,12],[12,13],
				[13,6],[13,7],[13,8],[13,9],[13,10],[13,11],[13,12],[13,13],
				];
			var arr_love = [
				[2,6],[2,7],[2,12],[2,13],
				[3,5],[3,6],[3,7],[3,8],[3,11],[3,12],[3,13],[3,14],
				[4,4],[4,5],[4,8],[4,9],[4,10],[4,11],[4,12],[4,13],[4,14],[4,15],
				[5,4],[5,5],[5,8],[5,9],[5,10],[5,11],[5,12],[5,13],[5,14],[5,15],
				[6,4],[6,5],[6,6],[6,8],[6,9],[6,10],[6,11],[6,12],[6,13],[6,14],[6,15],
				[7,5],[7,6],[7,7],[7,8],[7,9],[7,10],[7,11],[7,12],[7,13],[7,14],
				[8,6],[8,7],[8,8],[8,9],[8,10],[8,11],[8,12],[8,13],
				[9,7],[9,8],[9,9],[9,10],[9,11],[9,12],
				[10,8],[10,9],[10,10],[10,11],
				[11,9],[11,10],
				];
			var arr_U = [
				[2,5],[3,5],[4,5],[5,5],[6,5],[7,5],[8,5],[9,5],[10,5],[11,5],
				[2,6],[3,6],[4,6],[5,6],[6,6],[7,6],[8,6],[9,6],[10,6],[11,6],[11,7],
				[2,13],[3,13],[4,13],[5,13],[6,13],[7,13],[8,13],[9,13],[10,13],[11,13],[11,12],
				[2,14],[3,14],[4,14],[5,14],[6,14],[7,14],[8,14],[9,14],[10,14],[11,14],
				[12,6],[12,7],[12,8],[12,9],[12,10],[12,11],[12,12],[12,13],
				[13,7],[13,8],[13,9],[13,10],[13,11],[13,12],
				];

			var time = 5000;
			for(var i=0; i<300; i++){
				setTimeout(function(number){
					var elem = document.createElement('span');
					spanarr.push(elem);
					elem.style.cssText = "position: absolute;width: 40px;height: 40px;border-radius: 20px;";
					elem.style.cssText += "transition: all 3s;";
					elem.style.backgroundColor = 'hsla(0, 100%, '+(40 + Math.random()*40)+'%, 0.9)';
					elem.style.top = (40 * parseInt(number/20)) + "px";
					elem.style.left = (40 * parseInt(number%20)) + "px";
					div.appendChild(elem);
				}, i*time/300, i);
			}

			for(var i=0; i<8; i++){
				setTimeout(function(count){
					var color = (60*count)%360;
					spanarr.shuffle();

					for(var i=0; i<spanarr.length; i++){
						spanarr[i].style.top = 40*parseInt(i/20) + "px";
						spanarr[i].style.left = 40*parseInt(i%20) + "px";
						spanarr[i].style.backgroundColor = 'hsla('+color+', 100%, '+(40 + Math.random()*40)+'%, 0.9)';
					}
					//message
					if(count >= 5){
						var arr;
						switch(count){
							case 5:
								arr = arr_I;
								break;
							case 6:
								arr = arr_love;
								break;
							case 7:
								arr = arr_U;
								break;
						}
						var indexes = arr.map(function(obj){
							return obj[0]*20 + obj[1];
						});
						for(var i=0; i<spanarr.length; i++){
							//Don't use opacity. chrome for windows have bug.
							if(indexes.indexOf(i) == -1){
								spanarr[i].style.backgroundColor = 'hsla('+color+', 100%, '+(40 + Math.random()*40)+'%, 0.2)';
							}else{
								spanarr[i].style.backgroundColor = 'hsla('+color+', 100%, '+(40 + Math.random()*40)+'%, 0.8)';
							}
						}
					}
				}, time*(i+1), i);
			}

			setTimeout(function(){
				div.style.opacity = 0.0;
				setTimeout(function(){
					document.body.removeChild(div);
				}, 2000);
				objMain.style.opacity = 1.0;
				objLine.readOnly = false;
				objLine.focus();
				changeBackground('background.jpg');
			}, time*10);
		}, 20000);

	});
	_hyeseong.appendChild(iloveyou);
	DENYLIST.push("iloveyou");
	//Easter eggs end.

	//background preload
	changeBackground('background.jpg');
	init_terminal();

}, false);

function changeBackground(url){
	var img = new Image();
	img.onload = function(){
		document.body.style.background = 'url("' + url + '") no-repeat center center fixed';
	}
	img.src = url;
}
